<?php

class BaseData extends CActiveRecord {

    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /*
     *  查找数据的统一封装代码，不处理错误异常，直接抛给上层。一般可用于底层业务逻辑
     */
    public function findData($arr_field = null, $arr_cond = null, $arr_ne_cond = null, $arr_cond_in = null, $order_field = null, $is_asc = true, $offset = 0, $limit = 0, &$count = null) {

        $criteria = new CDbCriteria();

        // 设置查询字段
        if (empty($arr_field)) {
            $attributes = $this->getAttributes();
            $arr_field = array_keys($attributes);
        }
        $criteria->select = $arr_field;

        // 组装conditions
        $criteria->addColumnCondition($arr_cond);

        // 组装not equals conditions
        
        if (!empty($arr_ne_cond) && is_array($arr_ne_cond)) {
            
            foreach ($arr_ne_cond as $key => $value) {
                if (is_array($value)) {
                    throw new Exception('the value of arr_ne_cond in findData can not be an array', ErrorDef::DB_ERROR_CONDS);
                }
                $criteria->addCondition($key . '!=' . CDbCriteria::PARAM_PREFIX . CDbCriteria::$paramCount);
                $criteria->params [CDbCriteria::PARAM_PREFIX . CDbCriteria::$paramCount++] = $value;
            }
        }

        
        // 组装cond_in
        if (!empty($arr_cond_in) && is_array($arr_cond_in)) {
            foreach ($arr_cond_in as $key => $value) {
                if (!is_array($value)) {
                    throw new Exception('the value of arr_cond_in in findData must be an array', ErrorDef::DB_ERROR_CONDS);
                }
                $criteria->addInCondition($key, $value);
            }
        }

        $sort_method = $is_asc ? 'ASC' : 'DESC';

        // 组装order_field
        if (!empty($order_field)) {
            $criteria->order = $order_field . ' ' . $sort_method;
        }
        
        $count = $this->count($criteria);
        
        // 组装分页
        if ($limit > 0) {
            if ($offset < 0) {
                $offset = 0;
            }
            $criteria->offset = $offset;
            $criteria->limit = $limit;
        }        

        $ret = $this->findAll($criteria);        
        return $ret;
    }

    /*
     * 查找数据的错误处理版本，一般可用于中间业务逻辑
     */
    public function getData($arr_field = null, $arr_cond = null, $arr_ne_cond = null, $arr_cond_in = null, $order_field = null, $is_asc = true, $offset = 0, $limit = 0, &$count = null) {
        $ret = array('err' => ErrorDef::OK);
        try {
            $ret['data'] = $this->findData($arr_field, $arr_cond, $arr_ne_cond, $arr_cond_in, $order_field, $is_asc, $offset, $limit, $count);
        } catch (Exception $e) {
            $err_code = $e->getCode();
            if (empty($err_code)) {
                $ret['err'] = ErrorDef::COMMON_ERROR_SYS;
            } else {
                $ret['err'] = $err_code;
            }
            echo $e->getMessage();
        }
        return $ret;
    }
}
